<?php
require_once("../../../config.inc.php");
require_once("../../../app/_inc/include.php");
require_once(SERVER_FS_ROOT."/app/util/imex/_inc/importExport.func.php");
require_once("./_inc/entityFunction.php");

$errMsg = sxREQ('msg');

if(sxREQ("reqtype") == "daChanged"){	
	$_REQUEST['sa'] = '-all';
}
if(sxREQ("reqtype") == "save"){	
	$errMsg .= entityExp();
}
if(sxREQ("reqtype") == "newexp"){
	$errMsg .= newExp();		
}
$sSQL = "SELECT * FROM data_area ORDER BY data_area ";
$query = $objSDAO->query($sSQL,'da');
$aValDA = array();
while ($rowDAX = $objSDAO->fetch('da')) {
	$aValDA[$rowDAX['data_area']] = $rowDAX['data_area']." - ".$rowDAX['description'];
}
if (sxREQ('da')=='') {
	$_REQUEST['da'] = sxVx($objUser->aSV,'entityList','da');
}
if (sxREQ('da')=='') {
	reset($aValDA);
	$_REQUEST['da'] = key($aValDA);
}
$objUser->aSV['entityList']['da'] = sxREQ('da');

$sSQL = "SELECT subject_area, description FROM subject_area " .
		" WHERE data_area = '".sxREQ('da')."' ";
$sSQL .= " ORDER BY subject_area";
$objSDAO->query($sSQL,'SA');
$aValSA = array('-all'=>'-- all --');
while($rX=$objSDAO->fetch('SA')) {
	$aValSA[$rX['subject_area']] = $rX['subject_area']." - ".$rX['description'];
}
if (sxREQ('sa')=='') {
	$_REQUEST['sa'] = sxVx($objUser->aSV,'entityList','sa');
}
if (sxREQ('sa')=='') {
	$_REQUEST['sa'] = '-all';
}
$objUser->aSV['entityList']['sa'] = sxREQ('sa');

$dataArea = sxREQ('da');
$subjectArea = sxREQ('sa');

$sSQL = "SELECT e.* FROM entity e ";
if(sxREQ("sa")!='-all'){
	$sSQL .= " INNER JOIN subject_area_entity sa ON  sa.entity = e.entity " .
			" AND sa.data_area = '".sxRAS('da')."' ".
			" AND sa. subject_area = '".sxRAS('sa')."' ";
}
$sSQL .= " WHERE e.data_area = '".sxRAS('da')."' ORDER BY e.entity ";
$query = $objSDAO->query($sSQL);
$row = $objSDAO->fetch();

//output HTML
$aOpz = array("kMenu"=>'dam.ent');
echo(headerHTML($aOpz));
echo(bodyHTML($aOpz));
?>
<script language="JavaScript" type="text/javascript" src="../../_jsc/form.js"></script>  
<script language="JavaScript" type="text/javascript">
   var aElemObbl = Array();
   var aDescrElemObbl = Array();		
   function saChanged() {
  		document.objFrm.reqtype.value = 'saChanged';
  		document.objFrm.submit();
   }
   function daChanged() {
 		document.objFrm.reqtype.value = 'daChanged';
  		document.objFrm.submit();
   }
	function newExp() {
		document.objFrm.reqtype.value = 'newexp';
		document.objFrm.submit();
	}
	function selezExp() {
		for(k=1;k<1000;k++) {
			oExp = document.getElementById('exp_'+String(k));
			if (!oExp) break;
			oExp.checked = true;
		}
	}
	function deSelezExp() {
		for(k=1;k<1000;k++) {
			oExp = document.getElementById('exp_'+String(k));
			if (!oExp) break;
			oExp.checked = false;
		}
	}
</script>
<?
echo getHtmlForm('objFrm');
echo getHtmlHidden('reqtype','...');
echo getHtmlHidden('xsn',XSN);
echo("<table class='tabl01' width=\"100%\" cellpadding=\"3\" cellspacing=\"2\">\n");
$optDA = array('onChange'=>'daChanged();');	
echo("<tr>" .
		"<td class=\"labl01\" width='10%'>Data-Area: </td>" .
		"<td class=\"text01\" width='90%'>".
		getHtmlSelect('da',$aValDA,'@REQ',$optDA)."</td></tr>");
$optSA = array('onChange'=>'saChanged();');	
echo("<tr>" .
		"<td class=\"labl01\" width='10%'>Subject-Area: </td>" .
		"<td class=\"text01\" width='90%'>".
		getHtmlSelect('sa',$aValSA,'@REQ',$optSA)."</td></tr>\n");
echo("</table>\n");
	
//output comandi
$aCmd["sel"] = array("pag"=>"javascript:selezExp()",
					"title"=>_t("select all items"),
					"text"=>_t("select all"),"align"=>"L"
					);
$aCmd["des"] = array("pag"=>"javascript:deSelezExp()",
					"title"=>_t("unselect all items"),
					"text"=>_t("unselect all"),"align"=>"L"
					);

$aCmd["exp"] = array("pag"=>"javascript:newExp();",
									"title"=>_t("export"),
									"text"=>_t("export"),"align"=>"L",
									"perm"=>"M");
$aCmd["ins"] = array("pag"=>"entityIns.php?da=$dataArea",
									"title"=>_t("add new entity"),
									"text"=>_t("new entity"),
									"perm"=>"M");
$aCmd["back"] = array();
echo(commandsHTML($aCmd));	

echo("<table class='tabl01' width=\"100%\" cellpadding=\"3\" cellspacing=\"2\">\n");
echo("<tr>" .
		"<td class=\"titl01\"colspan=\"4\">List of entities</td>" .
	 "</tr>\n");
echo("<tr>" .
		"<td class=\"tabh01\" width=\"1%\">"._t("Export")."</td>" .
		"<td class=\"tabh01\" width=\"1%\">&nbsp;</td>" .
		"<td class=\"tabh01\" width=\"10%\">"._t("Entity")."</td>" .
		"<td class=\"tabh01\" width=\"88%\">"._t("Description")."</td>" .
	"</tr>\n");
$numElem = 0;
while($row){
	$numElem ++;
	echo("<tr>" .
			"<td class=\"text01\" align='center'>" .
			"<input class=\"text01\" type=\"checkbox\" name=\"exp_".$numElem."\" id=\"exp_".$numElem."\" value=\"".$row["entity"]."\"></td>" .
			"<td class=\"text01\"><a class=\"text01\" ".
			goToPag("../attr/attributeList.php?ent=".$row["entity"]."&da=".$row["data_area"]."&xsn=".XSN).
			" title=\""._t("View attributes")."\">".
			"<img src='".SERVER_URL_ROOT."/app/_img/b_props.png' border='0'>".
			"</a></td>" .
			"<td class=\"text01\"><a class=\"text01\" ".
			goToPag("../enty/entityView.php?ent=".$row["entity"]."&da=".$row["data_area"]."&xsn=".XSN).
			" title=\""._t("View entity")."\">".$row["entity"]."</a></td>" .
			"<td class=\"text01\">".sxHE($row["description"])."</td>" .
		"</tr>\n");
	$row = $objSDAO->fetch();
}
if($numElem == 0)
	echo("<tr>" .
			"<td class=\"text01\" colspan=\"4\">"._t("No items selecdted")."</td>".
		"</tr>\n");
echo("</table>\n");
echo(commandsHTML($aCmd));
echo("</td>\n</tr>\n</table>\n");
echo("</form>\n");
echo(footerHTML());
exit();

function newExp() {
	global $objUser;
	$aReqExp = array();
 	foreach($_REQUEST as $key=>$value) {
 		if(substr($key,0,4) == "exp_") {
  			$aReqExp[] = array('entity',$value);
  			$objUser->setHistoryVar(array($key=>''));
 		}
 	}
  	$objUser->setHistoryVar(array('reqtype'=>''));
	if (count($aReqExp) == 0) {
		return _t("no item selected");
	}
	$errMsg = tabExport($aReqExp);
	return $errMsg;
}

